#include "GY_LabelWidget.h"
#include "ui_GY_LabelWidget.h"
#include <QColor>
GY_LabelWidget::GY_LabelWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GY_LabelWidget)
{
    ui->setupUi(this);
}

GY_LabelWidget::~GY_LabelWidget()
{
    delete ui;
}
/**
 * @brief addLabelType 添加标签类型
 * @param typeName 类型名称
 * @param fontColor 字体颜色
 * @param background 背景颜色
 */
void GY_LabelWidget::addLabelType(const QString &typeName, const QColor &fontColor, const QColor &background)
{
    m_LabelTypeMap.insert(typeName,LabelColor());
    m_LabelTypeMap[typeName].fontColor=fontColor;
    m_LabelTypeMap[typeName].background=background;
}
/**
 * @brief addLabel 添加标签
 * @param labelContent 标签内容
 * @param typeName 标签类型
 */
void GY_LabelWidget::addLabel(const QString &labelContent, const QString &typeName)
{
    QLabel* pLabel=new QLabel(this);
    pLabel->setText(labelContent);
    m_LabelMap.insert(pLabel,typeName);
    QString styleString;
    if(typeName.isEmpty()||m_LabelTypeMap.contains(typeName))
    {
        styleString=QString("QLabel{background-color:rgb(255,255,255);color:rgb(0,0,0);}");
    }
    else
    {
        QColor fontColor=m_LabelTypeMap[typeName].fontColor;
        QColor background=m_LabelTypeMap[typeName].background;
        styleString=QString("QLabel{background-color:rgb(%1,%2,%3);color:rgb(%4,%5,%6);}")
                .arg(background.red()).arg(background.green()).arg(background.blue())
                .arg(fontColor.red()).arg(fontColor.green()).arg(fontColor.blue());
    }
    pLabel->setStyleSheet(styleString);//设置样式表
    ui->gridLayout_Content->addWidget(pLabel,m_LabelMap.size(),0,Qt::AlignLeft|Qt::AlignVCenter);
}
/**
 * @brief clearLabel
 * 清除标签
 */
void GY_LabelWidget::clearLabel()
{
    for (QMap<QLabel*,QString>::iterator it=m_LabelMap.begin();it!=m_LabelMap.end();it++)
    {
        it.key()->deleteLater();
    }
    m_LabelMap.clear();
}
